<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8">
        <title>用户列表</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" href="resources/layui/css/layui.css" media="all"/>
        <link rel="stylesheet" href="resources/css/public.css" media="all"/>
    </head>
    <body class="childrenBody">
        <form class="layui-form">
            <blockquote class="layui-elem-quote quoteBox">
                <form class="layui-form">
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">客户编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="custNo" id="custNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">客户名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="custName" id="custName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div>
                            <div class="layui-inline">
                                <label class="layui-form-label">客户地区</label>
                                <div class="layui-input-block">
                                    <select id="custRegion" name="custRegion" class="layui-input">
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">客户经理</label>
                                <div class="layui-input-block">
                                    <select id="custManagerName" name="custManagerName" class="layui-input">
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">客户等级</label>
                                <div class="layui-input-block">
                                    <select id="custLevel" name="custLevel" class="layui-input">
                                    </select>
                                </div>
                            </div>
                        </div>

                    </div>
                    <div>
                        <a class="layui-btn search_btn" data-type="reload">搜索</a>
                        <button type="reset" class="layui-btn">重置</button>
                        <!--<div class="layui-inline">
                            <a class="layui-btn layui-btn-normal addCustomer_btn">添加用户</a>
                        </div>-->
                        <div class="layui-inline">
                            <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
                        </div>
                    </div>
                </form>
            </blockquote>

            <script type="text/html" id="data-toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="export" id="exportFile">导出</button>
                </div>
            </script>

            <table id="customerList" lay-filter="customerList"></table>

            <!--操作-->
            <script type="text/html" id="customersListBar">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
                <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="linkman">联系人</a>
                <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="communication">交往记录</a>
                <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="order">订单</a>
            </script>
        </form>
      <!--  <script type="text/javascript" src="js/jquery-3.3.1.js"></script>-->
        <script type="text/javascript" src="resources/layui/layui.js"></script>
        <script type="text/javascript" src="resources/layui/layui_exts/excel.js"></script>
        <script th:inline="none">
            layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function () {
                var form = layui.form,
                    layer = parent.layer === undefined ? layui.layer : top.layer,
                    $ = layui.jquery,
                    laydate = layui.laydate,
                    laytpl = layui.laytpl,
                    table = layui.table;
                //用户列表
                var tableIns = table.render({
                    cellMinWidth: 80,
                    elem: '#customerList',
                    url: 'customers',
                    page: true,
                    height: "full-125",
                    limit: 20,
                    limits: [10, 15, 20, 25],
                    id: "customerListTable",
                    toolbar: "#data-toolbar",
                    defaultToolbar: ['filter', 'print', 'exports'],
                    totalRow: true,
                    title: '数据表',
                    cols: [[
                        {type: "checkbox", fixed: "left"},
                        {field: 'custNo', title: '顾客编号', align: "center"},
                        {field: 'custName', title: '顾客名称', align: "center"},
                        {
                            field: 'basicRegion', title: '地区', width: 150, align: "center", templet: function (row) {
                                if (row.basicRegion == null)
                                    return "";
                                return row.basicRegion.basiItem;
                            }
                        },
                        {
                            field: 'user', title: '客户经理负责人', width: 150, align: "center", templet: function (row) {
                                if (row.user == null)
                                    return "";
                                return row.user.realname;
                            }
                        },
                        {
                            field: 'basicLevel', title: '等级', width: 150, align: "center", templet: function (row) {
                                if (row.basicLevel == null)
                                    return "";
                                    return row.basicLevel.basiItem;
                            }
                        },
                        {field: 'custFlag', title: '标识',align:"center",templet:function (row) {
                                return row.custFlag==1?"启用":"禁用";
                            }},
                        {title: '操作', width: 400, toolbar: '#customersListBar', fixed: "right", align: "center"}
                    ]]
                });
                //搜索
                $(".search_btn").on("click", function () {
                    table.reload("customerListTable", {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        },
                        where: {
                            custName: $('#custName').val(),
                            custNo: $('#custNo').val(),
                            custRegionString: $('#custRegion').val(),
                            custManagerName: $('#custManagerName').val(),
                            custLevelString: $('#custLevel').val()

                        }
                    })
                });
                /*导出数据*/
                $('#exportFile').on('click',function () {
                    console.info("export");
                    $.ajax({
                        url: "customers",
                        type: 'get',
                        async: false,
                        dataType: 'json',
                        success: function (res) {
                            LAY_EXCEL.exportExcel(res.data, 'data.xlsx', 'xlsx')
                        }
                    });
                });

                table.on("tool(customerList)", function (obj) {
                    //获取当前行的数据
                    var data = obj.data;
                    switch (obj.event) {
                        case 'del':
                            deleteCustomer(data);
                            break;
                        case 'edit':
                            editCustomer(data);
                            break;
                        case 'linkman':
                            selectLinkman(data);
                            break;
                        case 'communication':
                            selectCommunication(data);
                            break;
                        case 'order':
                            selectOrder(data);
                            break;

                    }
                });

                //进入用户的编辑页面
                function editCustomer(data) {
                    var index = layui.layer.open({
                        title: "编辑用户",
                        type: 2,
                        content: "toCustomerEidtPage.do",
                        success: function (layero, index) {
                            var body = layui.layer.getChildFrame('body', index);//少了这个是不能从父页面向子页面传值的
                            body.contents().find(".custNo").val(data.custNo);
                            body.contents().find(".custName").val(data.custName);
                            body.contents().find(".custAddress").val(data.custAddress);
                            body.contents().find(".custZip").val(data.custZip);
                            body.contents().find(".custFax").val(data.custFax);
                            body.contents().find(".custTelephone").val(data.custTelephone);
                            body.contents().find(".custZip").val(data.custZip);
                            body.contents().find(".custWebsite").val(data.custWebsite);
                            body.contents().find(".custLicencNo").val(data.custLicencNo);
                            body.contents().find(".custCorporation").val(data.custCorporation);
                            body.contents().find(".custRegisteredCapital").val(data.custRegisteredCapital);
                            body.contents().find(".custTurnover").val(data.custTurnover);
                            body.contents().find(".custBank").val(data.custBank);
                            body.contents().find(".custBankAccount").val(data.custBankAccount);
                            body.contents().find(".custLocalTaxNo").val(data.custLocalTaxNo);
                            body.contents().find(".custNationalTaxNo").val(data.custNationalTaxNo);
                            if (data.available == 1) {
                                body.contents().find("input[type='radio']").eq(0).attr("checked", true);
                            } else if (data.available == 0) {
                                body.contents().find("input[type='radio']").eq(1).attr("checked", true);
                            }
                            //等级
                            var leavelValue = data.basicLevel.basiValue;
                            var html;
                            $.ajax({
                                url: '/grades/list.do',
                                success: function (data) {
                                    html += "<option value=''>------------请选择等级------------</option>"
                                    for (i = 0; i < data.length; i++) {
                                        if (leavelValue == data[i].basiValue) {
                                            html += ("<option value='" + data[i].basiValue + "' selected>" + data[i].basiItem + "</option>")
                                        }
                                        html += ("<option value='" + data[i].basiValue + "'>" + data[i].basiItem + "</option>")
                                    }
                                    body.contents().find("#customerGrades").append(html);
                                    form.render('select');
                                }
                            });

                            //经理
                            var managerValue = data.user.realname;
                            var html1;
                            $.ajax({
                                url: 'users/customerManager.do',
                                success: function (data) {
                                    html1 += "<option value=''>------------请选择经理------------</option>"
                                    for (i = 0; i < data.length; i++) {
                                        if (managerValue == data[i].realname) {
                                            html1 += ("<option value='" + data[i].realname + "'selected>" + data[i].realname + "</option>")
                                        }
                                        html1 += ("<option value='" + data[i].realname + "'>" + data[i].realname + "</option>")
                                    }
                                    body.contents().find("#custManager").append(html1);
                                    form.render('select');
                                }
                            });

                            //地区
                            var aresValue = data.basicRegion.basiValue;
                            var html3;
                            $.ajax({
                                url: '/ares/list.do',
                                success: function (data) {
                                    html3 += "<option value=''>------------请选择地区------------</option>"
                                    for (i = 0; i < data.length; i++) {
                                        if (aresValue == data[i].basiValue) {
                                            html3 += ("<option value='" + data[i].basiValue + "'selected>" + data[i].basiItem + "</option>")
                                        } else {
                                            html3 += ("<option value='" + data[i].basiValue + "'>" + data[i].basiItem + "</option>")
                                        }
                                    }
                                    body.contents().find("#customerAre").append(html3);
                                    form.render('select');
                                }
                            });


                        }
                    });
                    layui.layer.full(index);//全屏显示
                }


                //完成批量删除
                function deleteCustomer(data) {
                    $.ajax({
                        url: "customers/" + data.custNo,
                        type: "DELETE",
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 1002) {
                                layer.msg(res.msg);
                                tableIns.reload();//重新刷新表格
                            }
                        }
                    });
                }

                //批量删除
                $(".delAll_btn").click(function () {
                    var checkStatus = table.checkStatus('customerListTable');
                    var data = checkStatus.data;
                    var cCustNos = [];
                    if (data.length > 0) {
                        for (var i in data) {
                            cCustNos.push(data[i].custNo);
                        }
                        layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                            /*
                            $.get("/sys/deleteBatchUser.html",{ids:uIds},function (res) {
                                tableIns.reload();
                                layer.close(index);
                            });
                            */
                            $.ajax({
                                url: "customers/deleteBatch",
                                type: "GET",
                                data: {custNos: cCustNos},
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1002) {
                                        tableIns.reload();
                                        layer.close(index);
                                    }
                                }
                            });
                        })
                    } else {
                        layer.msg("请选择需要删除的用户");
                    }
                });

                //单击添加用户按钮
                $(".addCustomer_btn").click(function () {
                    var index = layui.layer.open({
                        title: "添加顾客",
                        type: 2,
                        content: "toCustomerAddPage.do",
                        success: function (layero, index) {
                            // var body = layui.layer.getChildFrame('body', index);
                        }
                    });
                    //全屏打开窗口
                    layui.layer.full(index);
                    window.sessionStorage.setItem("index", index);
                    //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
                    $(window).on("resize", function () {
                        layui.layer.full(window.sessionStorage.getItem("index"));
                    })
                });


                var html4;
                $.ajax({
                    url: '/ares/list.do',
                    success: function (data) {
                        html4 += "<option value=''>------------请选择------------</option>"
                        for (i = 0; i < data.length; i++) {
                            html4 += ("<option value='" + data[i].basiValue + "'>" + data[i].basiItem + "</option>")
                        }
                        $("#custRegion").append(html4);
                        form4.render('select');
                    }
                });

                var html5;
                $.ajax({
                    url: 'users/customerManager.do',
                    success: function (data) {
                        html5 += "<option value=''>------------请选择------------</option>"
                        for (i = 0; i < data.length; i++) {
                            html5 += ("<option value='" + data[i].realname + "'>" + data[i].realname + "</option>")
                        }
                        $("#custManagerName").append(html5);
                        form.render('select');
                    }
                });

                var html6;
                $.ajax({
                    url: '/grades/list.do',
                    success: function (data) {
                        html6 += "<option value=''>------------请选择------------</option>"
                        for (i = 0; i < data.length; i++) {
                            html6 += ("<option value='" + data[i].basiValue + "'>" + data[i].basiItem + "</option>")
                        }
                        $("#custLevel").append(html6);
                        form.render('select');
                    }
                });


                //联系人界面
                function selectLinkman(data) {
                    var index = layui.layer.open({
                        title: "查看联系人",
                        type: 2,
                        content: "toLinkmanPage.do",
                        id: "link",
                        success: function (layero, index) {
                            var body = layui.layer.getChildFrame('body', index);//少了这个是不能从父页面向子页面传值的
                            body.contents().find(".custNo").val(data.custNo);
                        }
                    });
                    layui.layer.full(index);//全屏显示
                }

                //交往记录界面
                function selectCommunication(data) {
                    var index1 = layui.layer.open({
                        title: "查看交往记录",
                        type: 2,
                        content: "toCommunicationPage.do",
                        id: "communication",
                        success: function (layero, index1) {
                            var body = layui.layer.getChildFrame('body', index1);//少了这个是不能从父页面向子页面传值的
                            body.contents().find(".custNo").val(data.custNo);
                        }
                    });
                    layui.layer.full(index1);//全屏显示
                }

                //顾客订单
                function selectOrder(data) {
                    var index2 = layui.layer.open({
                        title: "查看订单",
                        type: 2,
                        content: "toOrderPage.do",
                        id: "order",
                        success: function (layero, index2) {
                            var body = layui.layer.getChildFrame('body', index2);//少了这个是不能从父页面向子页面传值的
                            body.contents().find(".custNo").val(data.custNo);
                        }
                    });
                    layui.layer.full(index2);//全屏显示
                }
            })

        </script>
    </body>
</html>